// SPDX-FileCopyrightText: 2022 flovansl <co_sl@tutanota.com>
// SPDX-License-Identifier: MIT
import {
	DissassembleScroll
} from "./structs.slint";
import { UiIo } from "global.slint";

global Palette {
    out property <brush> background: #dfdfdf;
    out property <brush> foreground: #2d2d2d;
}

export component ScrollBar inherits Rectangle {
    in property <bool> enabled;
    out property <bool> has_hover: touch_area.has_hover;
    in_out property <bool> horizontal;
    in_out property <length> maximum;
    in_out property <length> page-size;
    in_out property <length> value;

    property <length> track_size: root.horizontal ? root.width - 2 * root.offset : root.height - 2 * offset;
    property <length> step_size: 10px;
    property <length> offset: 2px;
    property <length> pad: 2px;

    background: transparent;

    border := Rectangle {
        x: 0;
        y: 0;
        width: !root.horizontal ? 0.8px : parent.width;
        height: !root.horizontal ? parent.height : 0.8px;
        background: transparent;
    }

    thumb := Rectangle {
        width: !root.horizontal ? parent.width - 2 * root.pad : root.maximum <= 0phx ? 0phx : max(32px, root.track_size * root.page_size / (root.maximum + root.page_size));
        height: root.horizontal ? parent.height - 2 * root.pad : root.maximum <= 0phx ? 0phx : max(32px, root.track_size * (root.page_size / (root.maximum + root.page_size)));
        x: !root.horizontal ? (parent.width - self.width) / 2 : root.offset + (root.track_size - thumb.width) * (-root.value / root.maximum);
        y: root.horizontal ? (parent.height - self.height) / 2 : root.offset + (root.track_size - thumb.height) * (-root.value / root.maximum);
        border_radius: (root.horizontal ? self.height : self.width) / 2;
        background: Palette.foreground;
        opacity: 0.6;
        border_width: 0.8px;

        animate width, height { duration: 100ms; easing: ease_out; }
    }

    touch_area := TouchArea {
        property <length> pressed_value;

        width: parent.width;
        height: parent.height;

        double-clicked() => {
            UiIo.assemble-scroll(true, self.height, DissassembleScroll.ToPC);
        }

        pointer_event(event) => {
            if (event.button == PointerEventButton.left && event.kind == PointerEventKind.down) {
                self.pressed_value = -root.value;
            }
        }

        moved => {
            if (self.enabled && self.pressed) {
                root.value = -max(0px, min(root.maximum, self.pressed_value + (
                    root.horizontal ? (touch_area.mouse_x - touch_area.pressed_x) * (root.maximum / (root.track_size - thumb.width))
                               : (touch_area.mouse_y - touch_area.pressed_y) * (root.maximum / (root.track_size - thumb.height))
                )));
                UiIo.assemble-scrollbar-move(self.height, -root.value);
            }
        }
    }

    states [
        hover when touch_area.has_hover : {
            background: Palette.foreground.with_alpha(0.2);
            border.background: Palette.foreground.with_alpha(0.2);
            pad: 2px;
        }
    ]

    animate width, height, pad, background { duration: 100ms; easing: ease_out; }
}

export component ScrollView {
    min_width: 100px;
    min_height: 100px;
    horizontal_stretch: 1;
    vertical_stretch: 1;
    preferred_height: 100%;
    preferred_width: 100%;

    flickable := Flickable {
        interactive: true;
        width: 100%;
        height: 100%;

        @children
    }

    vertical_bar := ScrollBar {
        enabled: true;
        x: parent.width - self.width;
        y: 0;
        width: self.has_hover ? 16px : 8px;
        height: parent.height;
        horizontal: false;
        visible: UiIo.assemble_line_number > 0;
        maximum: UiIo.assemble_line_number * UiIo.assemble_line_height;
        page_size: flickable.height;
        value: -UiIo.assemble_line_current * UiIo.assemble_line_height;
    }
}